package com.ruoyi.station.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.oiltank.domain.OilTank;
import com.ruoyi.oiltank.mapper.OilTankMapper;
import com.ruoyi.oiltank.service.IOilTankService;
import com.ruoyi.oiltank.service.impl.OilTankServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.station.mapper.ReportGasStationMapper;
import com.ruoyi.station.domain.ReportGasStation;
import com.ruoyi.station.service.IReportGasStationService;

/**
 * 航空加油站数据上报Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@Service
public class ReportGasStationServiceImpl implements IReportGasStationService
{
    @Autowired
    private ReportGasStationMapper reportGasStationMapper;

    @Autowired
    private IOilTankService oilTankService;

    /**
     * 查询航空加油站数据上报
     *
     * @param id 航空加油站数据上报ID
     * @return 航空加油站数据上报
     */
    @Override
    public ReportGasStation selectReportGasStationById(Long id)
    {
        return reportGasStationMapper.selectReportGasStationById(id);
    }
    public  Integer countTodayRecordsStation( Long deptId){
        return reportGasStationMapper.countTodayRecordsStation(deptId);
    }
    /**
     * 查询航空加油站数据上报列表
     *
     * @param reportGasStation 航空加油站数据上报
     * @return 航空加油站数据上报
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ReportGasStation> selectReportGasStationList(ReportGasStation reportGasStation)
    {
        List<String> dateQuery = reportGasStation.getDateQuery();
        if (CollectionUtil.isNotEmpty(dateQuery)){
            String start = dateQuery.get(0);
            String end = dateQuery.get(1);
            reportGasStation.setStart(start);
            reportGasStation.setEnd(end);
        }
        return reportGasStationMapper.selectReportGasStationList(reportGasStation);
    }

    /**
     * 新增航空加油站数据上报
     *
     * @param reportGasStation 航空加油站数据上报
     * @return 结果
     */
    @Override
    public int insertReportGasStation(ReportGasStation reportGasStation)
    {
        Long dirtyTankId = reportGasStation.getDirtyTankId();
        OilTank oilTank = oilTankService.selectOilTankById(dirtyTankId);
        if (ObjectUtil.isNull(oilTank)){
            throw new RuntimeException("查询不到储罐信息");
        }
        oilTank.setId(dirtyTankId);
        BigDecimal dirtyOilTankWeight = reportGasStation.getDirtyOilTankWeight();
        BigDecimal dirtyOilTankHeight = reportGasStation.getDirtyOilTankHeight();
        oilTank.setWeight(dirtyOilTankWeight);
        oilTank.setHeight(dirtyOilTankHeight);
        oilTankService.updateOilTank(oilTank);
        return reportGasStationMapper.insertReportGasStation(reportGasStation);
    }

    /**
     * 修改航空加油站数据上报
     *
     * @param reportGasStation 航空加油站数据上报
     * @return 结果
     */
    @Override
    public int updateReportGasStation(ReportGasStation reportGasStation)
    {
        Long dirtyTankId = reportGasStation.getDirtyTankId();
        OilTank oilTank = oilTankService.selectOilTankById(dirtyTankId);
        if (ObjectUtil.isNull(oilTank)){
            throw new RuntimeException("查询不到储罐信息");
        }
        oilTank.setId(dirtyTankId);
        BigDecimal dirtyOilTankWeight = reportGasStation.getDirtyOilTankWeight();
        BigDecimal dirtyOilTankHeight = reportGasStation.getDirtyOilTankHeight();
        oilTank.setWeight(dirtyOilTankWeight);
        oilTank.setHeight(dirtyOilTankHeight);
        oilTankService.updateOilTank(oilTank);
        return reportGasStationMapper.updateReportGasStation(reportGasStation);
    }

    /**
     * 批量删除航空加油站数据上报
     *
     * @param ids 需要删除的航空加油站数据上报ID
     * @return 结果
     */
    @Override
    public int deleteReportGasStationByIds(Long[] ids)
    {
        return reportGasStationMapper.deleteReportGasStationByIds(ids);
    }

    /**
     * 删除航空加油站数据上报信息
     *
     * @param id 航空加油站数据上报ID
     * @return 结果
     */
    @Override
    public int deleteReportGasStationById(Long id)
    {
        return reportGasStationMapper.deleteReportGasStationById(id);
    }
}
